package com.jams.goodox.utils.alipay;

import android.util.Log;

import com.jams.goodox.constant.Constants;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Result {

	private static final Map<String, String> sResultStatus;

	private String mResult;

	public String resultStatus = null;
	public String memo = null;
	public String result = null;
	public boolean isSignOk = false;
	public boolean success = false;

	public Result(String result) {
		this.mResult = result;
	}

	static {
		sResultStatus = new HashMap<String, String>();
		sResultStatus.put("9000", "支付成功");
		sResultStatus.put("8000", "支付结果确认中");
		sResultStatus.put("4000", "系统异常");
		sResultStatus.put("4001", "数据格式不正确");
		sResultStatus.put("6001", "用户中途取消支付操作");
	}

	public String getResult() {
		String src = mResult.replace("{", "");
		src = src.replace("}", "");
		return getContent(src, "memo=", ";result");
	}

	public void parseResult() {

		try {
			String src = mResult.replace("{", "");
			src = src.replace("}", "");
			String rs = getContent(src, "resultStatus=", ";memo");
			// if (sResultStatus.containsKey(rs)) {
			// resultStatus = sResultStatus.get(rs);
			// } else {
			// resultStatus = "其他错误";
			// }
			// resultStatus += "(" + rs + ")";
			resultStatus = rs;

			memo = getContent(src, "memo=", ";result");
			result = getContent(src, "result=", null);
			isSignOk = checkSign(result);
			success = checkSuccess(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private boolean checkSuccess(String result) {
		boolean retVal = false;
		try {
			JSONObject json = string2JSON(result, "&");

			String success = json.getString("success");
			success = success.replace("\"", "");

			if (success.equalsIgnoreCase("true") || success == "true") {
				retVal = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.i("Result", "Exception =" + e);
		}
		Log.i("Result", "checkSign =" + retVal);
		return retVal;
	}

	private boolean checkSign(String result) {
		boolean retVal = false;
		try {
			JSONObject json = string2JSON(result, "&");

			int pos = result.indexOf("&sign_type=");
			String signContent = result.substring(0, pos);

			String signType = json.getString("sign_type");
			signType = signType.replace("\"", "");

			String sign = json.getString("sign");
			sign = sign.replace("\"", "");

			if (signType.equalsIgnoreCase("RSA")) {
				retVal = Rsa.doCheck(signContent, sign, Constants.RSA_PUBLIC);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.i("Result", "Exception =" + e);
		}
		Log.i("Result", "checkSign =" + retVal);
		return retVal;
	}

	public JSONObject string2JSON(String src, String split) {
		JSONObject json = new JSONObject();

		try {
			String[] arr = src.split(split);
			for (String anArr : arr) {
				String[] arrKey = anArr.split("=");
				json.put(arrKey[0], anArr.substring(arrKey[0].length() + 1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return json;
	}

	private String getContent(String src, String startTag, String endTag) {
		String content = src;
		int start = src.indexOf(startTag);
		start += startTag.length();

		try {
			if (endTag != null) {
				int end = src.indexOf(endTag);
				content = src.substring(start, end);
			} else {
				content = src.substring(start);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return content;
	}
}
